package com.dreamchaser.familydevice.config.device.infunction;

import com.dreamchaser.familydevice.annotation.Function;
import com.dreamchaser.familydevice.config.SpringContextHolder;
import com.dreamchaser.familydevice.constant.DeviceCache;
import com.dreamchaser.familydevice.util.device.ByteHexUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 *
 *
 * @author: SJie
 * @Date: 2020/8/22 16:50
 * @description:
 *
 */
@Function(value = "delay", descript = "延时执行函数",format = "delay(*)")
@Slf4j
public class Delay implements IFunction {
    @Override
    public boolean done(int deviceCode, String value, int data) {
        int _value = Integer.valueOf(value);
        String taskName = "delay" + ByteHexUtil.hexToHexStr(deviceCode) + ByteHexUtil.hexToHexStr(data);
        log.info(deviceCode + "号设备指令:"+ByteHexUtil.hexToHexStr(data)+" 入队列，延时" + _value + "秒");
        StringRedisTemplate stringRedisTemplate=SpringContextHolder.getBean(StringRedisTemplate.class);
        stringRedisTemplate.opsForValue().set(taskName, "1", _value, TimeUnit.SECONDS);
        DeviceCache.taskName.put(taskName, data);
        return false;

    }
}
